package com.bootdo.appsystem.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.appsystem.domain.*;
import com.bootdo.appsystem.service.*;
import com.bootdo.common.domain.FileDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author xiaoxiao
 * @email 806191255@qq.com
 * @date 2018-05-12 14:42:14
 */
 
@Controller
@RequestMapping("/appsystem/seller")
public class SellerController {
	@Autowired
	private SellerService sellerService;
	@Autowired
	private RegectService regectService;
	@Autowired
	private UseraccountService useraccountService;
	@Autowired
	private ImageinfoService imageinfoService;
	@Autowired
	private SellercategoryService sellercategoryService;
	
	@GetMapping()
	@RequiresPermissions("appsystem:seller:seller")
	String Seller(){
	    return "appsystem/seller/seller";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("appsystem:seller:seller")
	public PageUtils list(@RequestParam Map<String, Object> params){
		if("0".equals(params.get("provincename"))){
			params.put("provincename",null);
		}
		if("0".equals(params.get("cityname"))){
			params.put("cityname",null);
		}
		if("0".equals(params.get("districtnname"))){
			params.put("districtnname",null);
		}
		String uid = (String) params.get("uid");
		if(uid != null && !uid.equals("")){
			params.put("uid",Integer.parseInt(uid) - 10000);
		}
		//查询列表数据
        Query query = new Query(params);
		List<SellerDO> sellerList = sellerService.list(query);
		int total = sellerService.count(query);
		PageUtils pageUtils = new PageUtils(sellerList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("appsystem:seller:add")
	String add(){
	    return "appsystem/seller/add";
	}

	@GetMapping("/edit/{sellerid}")
	@RequiresPermissions("appsystem:seller:edit")
	String edit(@PathVariable("sellerid") Integer sellerid,Model model){

		SellerDO seller = sellerService.get(sellerid);

		//区县
		RegectDO quxian = regectService.get(seller.getRegectid().doubleValue());
		List<RegectDO> quxianList = regectService.creatCity(quxian.getParentId()).getData();
		model.addAttribute("quxian",quxian);
		model.addAttribute("quxianList",quxianList);
		//市级
		RegectDO shi = regectService.get(quxian.getParentId());
		List<RegectDO> shiList = regectService.creatCity(shi.getParentId()).getData();
		model.addAttribute("shi",shi);
		model.addAttribute("shiList",shiList);
		//省级
		RegectDO sheng = regectService.get(shi.getParentId());
		List<RegectDO> shengList = regectService.creatCity(1.0).getData();
		model.addAttribute("sheng",sheng);
		model.addAttribute("shengList",shengList);
		//商家分类
		List<SellercategoryDO> categorylist = sellercategoryService.list(null);
		model.addAttribute("categorylist",categorylist);

		model.addAttribute("seller", seller);
		return "appsystem/seller/edit";
	}

	@GetMapping("/editImage/{sellerid}")
	@RequiresPermissions("appsystem:seller:edit")
	String editImage(@PathVariable("sellerid") Integer sellerid,Model model){
		SellerDO seller = sellerService.get(sellerid);
		model.addAttribute("seller", seller);
		return "appsystem/seller/editImage";
	}

	@ResponseBody
	@GetMapping("/listImages")
	@RequiresPermissions("appsystem:seller:editimages")
	public PageUtils listImages(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		query.put("type",10006);
		List<ImageinfoDO> imageinfoDOList = imageinfoService.list(query);
		int total = imageinfoService.count(query);
		PageUtils pageUtils = new PageUtils(imageinfoDOList, total);
		return pageUtils;
	}

	@GetMapping("/ditu/{sellerid}")
	@RequiresPermissions("appsystem:seller:edit")
	String ditu(@PathVariable("sellerid") Integer sellerid , Model model){
		SellerDO seller = sellerService.get(sellerid);
		model.addAttribute("seller", seller);
		return "appsystem/seller/ditu";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("appsystem:seller:add")
	public R save( SellerDO seller){
		if(sellerService.save(seller)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("appsystem:seller:edit")
	public R update( SellerDO seller){
        UseraccountDO useraccountDO = useraccountService.get(seller.getUid());
        if(useraccountDO != null && useraccountDO.getSellerid() == null){
            UseraccountDO user = new UseraccountDO();
            user.setSellerid(seller.getSellerid());
            user.setUid(seller.getUid());
            useraccountService.update(user);
		}
		sellerService.update(seller);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("appsystem:seller:remove")
	public R remove( Integer sellerid){
		if(sellerService.remove(sellerid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("appsystem:seller:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] sellerids){
		sellerService.batchRemove(sellerids);
		return R.ok();
	}








//	未审核商家相关
	//商家审核列表
	@GetMapping("/shenheSeller")
	@RequiresPermissions("appsystem:seller:shenhelist")
	String shenheSeller(){
		return "appsystem/seller/shenheSeller";
	}

	//商家未审核列表数据
	@ResponseBody
	@GetMapping("/shenhelist")
	@RequiresPermissions("appsystem:seller:shenhelist")
	public PageUtils shenhelist(@RequestParam Map<String, Object> params){
		if("0".equals(params.get("provincename"))){
			params.put("provincename",null);
		}
		if("0".equals(params.get("cityname"))){
			params.put("cityname",null);
		}
		if("0".equals(params.get("districtnname"))){
			params.put("districtnname",null);
		}
		String uid = (String) params.get("uid");
		if(uid != null && !uid.equals("")){
			params.put("uid",Integer.parseInt(uid) - 10000);
		}
		//未审核商家
		params.put("status","0");
		//查询列表数据
		Query query = new Query(params);
		List<SellerDO> sellerList = sellerService.list(query);
		int total = sellerService.count(query);
		PageUtils pageUtils = new PageUtils(sellerList, total);
		return pageUtils;
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/shenheupdate")
	@RequiresPermissions("appsystem:seller:shenheedit")
	public R shenheupdate( SellerDO seller){
		sellerService.update(seller);
		return R.ok();
	}

	@GetMapping("/shenheedit/{sellerid}")
	@RequiresPermissions("appsystem:seller:shenheedit")
	String shenheedit(@PathVariable("sellerid") Integer sellerid,Model model){
		SellerDO seller = sellerService.get(sellerid);

		//区县
		RegectDO quxian = regectService.get(seller.getRegectid().doubleValue());
		List<RegectDO> quxianList = regectService.creatCity(quxian.getParentId()).getData();

		//市级
		RegectDO shi = regectService.get(quxian.getParentId());
		List<RegectDO> shiList = regectService.creatCity(shi.getParentId()).getData();

		//省级
		RegectDO sheng = regectService.get(shi.getParentId());
		List<RegectDO> shengList = regectService.creatCity(1.0).getData();

		model.addAttribute("quxian",quxian);
		model.addAttribute("quxianList",quxianList);
		model.addAttribute("shi",shi);
		model.addAttribute("shiList",shiList);
		model.addAttribute("sheng",sheng);
		model.addAttribute("shengList",shengList);

		//商家分类
		List<SellercategoryDO> categorylist = sellercategoryService.list(null);
		model.addAttribute("categorylist",categorylist);

		model.addAttribute("seller", seller);
		return "appsystem/seller/shenheedit";
	}







}
